package com.lanou.dllo.vc.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.dllo.vc.R;

import butterknife.Bind;
import butterknife.ButterKnife;

public class CreateGroupActivty extends BaseActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {
    @Bind(R.id.group_Introduction)
    EditText group_Introduction;//群聊简洁
    @Bind(R.id.groupname) //群聊名称
    EditText groupname;
    @Bind(R.id.createSave)
    TextView createSave;
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.isInvitation)
    CheckBox isInvitation;
    @Bind(R.id.isOpen)
    CheckBox isOpen;


    private String groupName, desc;
    private String[] members;
    private boolean allowInvite = false;
    private boolean is_open = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_create_group_activty;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        isInvitation.setOnCheckedChangeListener(this);
        isOpen.setOnCheckedChangeListener(this);
        createSave.setOnClickListener(this);
    }

    @Override
    protected void initData() {

        back.setOnClickListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.isInvitation:
                //:是否允许群成员邀请人进群
//                try {
//                    EMGroupManager.getInstance().createPrivateGroup
//                            (groupName, desc, members,allowInvite);//需异步处理
//                } catch (EaseMobException e) {
//                    e.printStackTrace();
//                }
                allowInvite = isChecked;
                break;
            case R.id.isOpen:
                is_open = isChecked;
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.createSave:
                createGroup();
                break;

            case R.id.back:

                finish();
                break;
        }
    }

    private void createGroup() {
        groupName = groupname.getText().toString();
        desc = group_Introduction.getText().toString();
        Intent intent = new Intent(this, FriendsListActivity.class);
        intent.putExtra("groupId", groupName);
        startActivityForResult(intent, 20);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 20) {
            final String[] m = data.getStringArrayExtra("members");

            if (is_open) {//公开群
                //前面三个参数和创建私有群一致
//needApprovalRequired:如果创建的公开群用需要户自由加入，就传false。否则需要申请，等群主批准后才能加入，传true
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Log.i("lanou", "groupName---->" + groupName);
                            EMGroupManager.getInstance().createPublicGroup(groupName, desc, m, false);//需异步处理
                        } catch (EaseMobException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();


            } else {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //groupName：要创建的群聊的名称
//desc：群聊简介
//members：群聊成员,为空时这个创建的群组只包含自己
//allowInvite:是否允许群成员邀请人进群
                        try {
                            EMGroupManager.getInstance().createPrivateGroup(groupName, desc, m, allowInvite);//需异步处理
                        } catch (EaseMobException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();


            }
            Intent intent = new Intent();
            intent.putExtra("isFresh", true);
            intent.putExtra("group",groupName);
            setResult(RESULT_OK, intent);
            finish();
        }

    }
}
